package config;

import com.baomidou.mybatisplus.generator.config.rules.DateType;
import lombok.Data;

/**
 * @Description PACKAGE_NAME
 * @Author zhengzetao
 * @Date 2022/9/16 15:52
 */
@Data
public class GlobalConfig extends Config {

    final private static String GROUP = "GlobalConfig";

    //fileOverride	覆盖已生成文件	默认值:false
    private boolean fileOverride;
    //disableOpenDir	禁止打开输出目录	默认值:false
    private boolean disableOpenDir;
    //outputDir(String)	指定输出目录	/opt/baomidou/ 默认值: windows:D:// linux or mac : /tmp
    private String outputDir;
    //author(String)	作者名	baomidou 默认值:作者
    private String author;
    //enableKotlin	开启 kotlin 模式	默认值:false
    private boolean enableKotlin;
    //enableSwagger	开启 swagger 模式	默认值:false
    private boolean enableSwagger;
    //dateType(DateType)	时间策略	DateType.ONLY_DATE,DateType.SQL_PACK,DateType.TIME_PACK 默认值: DateType.TIME_PACK
    private DateType dateType;
    //commentDate(String)	注释日期	默认值: yyyy-MM-dd
    private String commentDate;

    private GlobalConfig() {
        super(GROUP);
        this.fileOverride = getBoolDefaultFalse("fileOverride");
        this.disableOpenDir = getBoolDefaultFalse("disableOpenDir");
        this.outputDir = getStr("outputDir", ".\\generator");
        this.author = getStr("author", "author");
        this.enableKotlin = getBoolDefaultFalse("enableKotlin");
        this.enableSwagger = getBoolDefaultFalse("enableSwagger");
        switch (getStr("dateType", "TIME_PACK")) {
            case "ONLY_DATE" -> this.dateType = DateType.ONLY_DATE;
            case "SQL_PACK" -> this.dateType = DateType.SQL_PACK;
            case "TIME_PACK" -> this.dateType = DateType.TIME_PACK;
        }
        this.commentDate = getStr("commentDate", "yyyy-MM-dd");
    }

    public static void setting(com.baomidou.mybatisplus.generator.config.GlobalConfig.Builder builder) {
        GlobalConfig globalConfig = new GlobalConfig();
        builder.author(globalConfig.author).outputDir(globalConfig.outputDir).dateType(globalConfig.dateType).commentDate(globalConfig.commentDate);
        if (globalConfig.fileOverride) {
            builder.fileOverride();
        }
        if (globalConfig.disableOpenDir) {
            builder.disableOpenDir();
        }
        if (globalConfig.enableKotlin) {
            builder.enableKotlin();
        }
        if (globalConfig.enableSwagger) {
            builder.enableSwagger();
        }
    }
}
